﻿using ProjScan.ScanRuleFixer.Utils;
using ProjScan.Tools;
using System;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager.AssetData
{
    internal class AudioClipAssetData : ImportAssetData<AudioClip, AudioImporter>
    {
        public enum AudioClipLoadType
        {
            DecompressOnLoad,
            CompressedInMemory,
            Streaming,
            Unknown
        }

        public float clipLength;

        public AudioClipLoadType clipLoadType;

        public AudioImporterSampleSettings importSampleSettings;

        public AudioCompressionFormat compressionFormat;

        public AudioSampleRateSetting sampleRateSetting;

        public float qualityData;

        public int channelCount;

        public AudioClipAssetData(int directoryId, string filePath, AudioClip audioClipClip, AudioImporter audioImporter)
            : base(directoryId, filePath, audioClipClip, audioImporter)
        {
            if (audioImporter == null)
            {
                noImporter = true;
            }
            else
            {
                string currentPlatform = ProjScan.ScanRuleFixer.Utils.Tools.GetCurrentPlatform();
                if (audioImporter.ContainsSampleSettingsOverride(currentPlatform))
                {
                    importSampleSettings = audioImporter.GetOverrideSampleSettings(currentPlatform);
                }
                else
                {
                    importSampleSettings = audioImporter.defaultSampleSettings;
                }

                compressionFormat = importSampleSettings.compressionFormat;
                sampleRateSetting = importSampleSettings.sampleRateSetting;
                if (compressionFormat == AudioCompressionFormat.Vorbis || compressionFormat == AudioCompressionFormat.MP3)
                {
                    qualityData = importSampleSettings.quality;
                }
            }

            clipLength = audioClipClip.length;
            clipLoadType = AudioClipLoadType.Unknown;
            object propertyValue = AssemblyTools.I.GetPropertyValue(audioClipClip, "loadType");
            if (propertyValue != null)
            {
                string value = propertyValue.ToString();
                if (!string.IsNullOrEmpty(value))
                {
                    clipLoadType = (AudioClipLoadType)Enum.Parse(typeof(AudioClipLoadType), value);
                }
            }

            object[] parameters = new object[1]
            {
                audioClipClip
            };
            AssemblyTools.I.TryGetMethodResult("UnityEditor.AudioUtil", "GetChannelCount", out channelCount, parameters);
        }

        public override string GetCSVHeader()
        {
            return "Name,DirID,Time,Quality";
        }

        public override string GetInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(ToolsHelper.QuoteString(fileName));
            stringBuilder.Append(',');
            stringBuilder.Append(assetPathId);
            stringBuilder.Append(',');
            stringBuilder.Append(clipLength);
            stringBuilder.Append(',');
            stringBuilder.Append(Math.Round(qualityData * 100f));
            return stringBuilder.ToString();
        }
    }
}